import request from '@/utils/request'
import type { Data } from '@/utils/request'
import type { userPwdInter, userInfoInter, registerForm, loginForm } from '@/types/user'

// 用户注册
export const userRegisterService = (registerData: registerForm) => {
  const params = new URLSearchParams()
  Object.entries(registerData).forEach(([key, value]) => {
    if (value !== null && value !== undefined) {
      params.append(key, String(value));
    }
  });
  return request.post<Data<object>>("/user/register", params.toString(), {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  })
}
// 用户登录
export const userLoginService = (loginData: loginForm) => {
  const params = new URLSearchParams()
  Object.entries(loginData).forEach(([key, value]) => {
    if (value !== null && value !== undefined) {
      params.append(key, String(value));
    }
  });
  return request.post<Data<string>>("/user/login", params.toString(), {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  })
}
// 获取用户信息
export const userInfoService = () => {
  return request.get<Data<userInfoInter>>("/user/userInfo")
}
// 更新用户信息
export const userInfoUpdateService = (userInfoData: userInfoInter) => {
  return request.put<Data<object>>("/user/update", userInfoData)
}
// 更换密码
export const userPwdUpdateService = (userPwdData: userPwdInter) => {
  return request.patch<Data<object>>("/user/updatePwd", userPwdData)
}